<template>
    <div id="register">
        <el-container>
            <el-header>
                <span>用户注册</span>
            </el-header>
            <el-main>
             <el-form ref="form" :model="form" label-width="140px" status-icon :rules="rules">
                <div class="d">                  
                 <el-form-item label="角色">
                 <el-col :span="20">
                 <el-select v-model="form.roleId" placeholder="请选择角色">
                  <el-option label="商户" value="1"></el-option>
                  <el-option label="店长" value="2"></el-option>
                 
                 </el-select>
                 </el-col>                  
                 </el-form-item>
                <el-form-item label="账号" prop="username">
                <el-col :span="20">
                 <el-input v-model="form.username"></el-input>
                </el-col>            
              </el-form-item>
              <el-form-item label="手机号" prop="telephone">
                <el-col :span="20">
              <el-input type="text" v-model="form.telephone" autocomplete="off" ></el-input>
                </el-col> 
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-col :span="20">
              <el-input type="password" v-model="form.password" autocomplete="off"></el-input>
                </el-col> 
              </el-form-item>
              <el-form-item label="确认密码" prop="checkPass">
                <el-col :span="20">
              <el-input type="password" v-model="form.checkPass" autocomplete="off"></el-input>
                </el-col>
              </el-form-item>  
                <el-form-item>
                  <el-col :span="20">
                    <el-button type="primary" @click="onSubmit">注册</el-button>
                    <el-button @click="back">取消</el-button>
                  </el-col>
                </el-form-item>
                </div>
              
                </el-form>
            </el-main>
            <el-footer></el-footer>
        </el-container>
    </div>
</template>
<script>
    export default {
      data() {
        var validatePass = (rule, value, callback) => {
          console.log(value);
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.form.checkPass !== '') {
            this.$refs.form.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePhone = (rule,value,callback)=> {
        console.log(value);
        if (value === '') {
          callback(new Error('请输入手机号'));
        } else {
          const regMobile = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
          if (!regMobile.test(value)) {
            callback(new Error('手机号格式不对'));
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        console.log(value);
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.form.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
        return {
          form: {    
            roleId: '',     
            username:'',
            password: '',
            checkPass: '',
            telephone: ''
          },
          options: [],
          roles: [],
          rules: {
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        telephone: [
          { validator: validatePhone, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
        }
      },
      
      methods: {
        onSubmit() {
          console.log(this.form);
          this.$axios.post("http://localhost:8082/addUser",this.form)
          .then(response =>{
              if(response.data.code!='200'){
                this.$message.error("注册失败：" + response.data.message);
              }else{
                this.$message("注册成功");
                this.$router.push("/principal");
              }
          })
          .catch(error =>{
              this.$message.error("系统错误");
          })
          
        },
        rowStyle() {
          return "text-align:center";
        },
        back() {
          this.$router.push("/principal");
        },

      }
    }
  </script>
  <<style>
   .d {
        margin: auto;
        width: 700px;
        
   }
  </style>